package com.cloudsigns.factorymanager2.engineering.activity;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolCompletionForSuperView;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.AddCompletionOrderCommentPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolCompletionForSuperPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.activity.AddCompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.List;

/**
 * 巡查完结单 超级管理员
 * Created by tony on 2018/5/11.
 */

public class PatrolCompletionOrderForSuperActivity extends Activity implements View.OnClickListener, IPatrolOrderDetailView, IPatrolCompletionForSuperView {
    private TextView tv_title;
    private ViewGroup rl_back;
    private TextView edt_region_name, edt_media_number, edt_patrol_cycle, edt_patrol_company, edt_patrol_responsible;
    private TextView edt_handle_account, ed_handle_date;
    private TextView btn_confirm_evaluation;
    private boolean isclickable;
    private View rl_handle_account, rl_handle_date;
    private final String TAG = PatrolCompletionOrderForSuperActivity.class.getSimpleName();
    private OrderParentBean orderParentBean;
    private PatrolOrderDetailPresenter patrolOrderDetailPresenter;
    private PatrolOrderBean patrolOrderBean;
    private PatrolCompletionForSuperPresenter patrolCompletionForSuperPresenter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_partrol_completion_order_for_super);

        patrolOrderDetailPresenter = new PatrolOrderDetailPresenter(this);
        patrolCompletionForSuperPresenter = new PatrolCompletionForSuperPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);

            if (null != orderParentBean) {
                edt_region_name.setText(orderParentBean.getProjectName());
            }

        }


        patrolOrderDetailPresenter.getPatrolOrderDetail();
    }

    private void init() {
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        edt_handle_account = findViewById(R.id.edt_handle_account);
        ed_handle_date = findViewById(R.id.ed_handle_date);
        edt_region_name = findViewById(R.id.edt_region_name);
        edt_media_number = findViewById(R.id.edt_media_number);
        edt_patrol_cycle = findViewById(R.id.edt_patrol_cycle);
        edt_patrol_company = findViewById(R.id.edt_patrol_company);
        edt_patrol_responsible = findViewById(R.id.edt_patrol_responsible);
        btn_confirm_evaluation = findViewById(R.id.btn_confirm_evaluation);


        tv_title.setText(getResources().getText(R.string.completion_order));
        rl_back.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);


    }

    private void initData() {
        if (null != patrolOrderBean) {
            edt_region_name.setText(patrolOrderBean.getRegionName());
            List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
            if (null != mediaBeanList)
                edt_media_number.setText(mediaBeanList.size() + "");

            edt_patrol_cycle.setText(DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getBeginDate()) + "/" + DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getCompleteDate()));
            edt_patrol_company.setText(patrolOrderBean.getCompanyName());
            edt_patrol_responsible.setText(patrolOrderBean.getReceiveAccountId() + ":" + patrolOrderBean.getReceiveAccountName());

//            edt_handle_account.setText(patrolOrderBean.getCreateOrderAccountName());

            CommonEnum.CompleteStatus completeStatus = patrolOrderBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClickable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClickable(false);
                }
            }

        }
    }


    private void btnClickable(boolean bool) {
        isclickable = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    private void submitCompletion() {
        patrolCompletionForSuperPresenter.patrolCompletionForSuper();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_confirm_evaluation:
                if (isclickable)
                    submitCompletion();
                break;

        }
    }


    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void patrolCompletionForSuperSuccess() {
        Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void patrolCompletionForSuperFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getPatrolOrderDetailSuccessed(PatrolOrderBean orderBean) {
        patrolOrderBean = orderBean;
        initData();
    }

    @Override
    public void gPatrolOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
